home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / ps_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  5.0 KB  |  205 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  ps_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_PSTREE_H
  16. #define LEDA_PSTREE_H
  17.  
  18. //--------------------------------------------------------------------
  19. //  
  20. //  Priority Search Trees
  21. //
  22. //  Renate Lassen (1990)
  23. //
  24. // Implementation as described in
  25. // Kurt Mehlhorn: Data Structures and Algorithms 2, section III.5.1.2
  26. //
  27. //--------------------------------------------------------------------
  28.  
  29. // -----------------------------------------------------
  30. // includes
  31. // -----------------------------------------------------
  32.  
  33. #include <LEDA/basic.h>
  34. #include <LEDA/b_stack.h>
  35.  
  36.  
  37. // -----------------------------------------------------
  38. // declarations and definitions
  39. // -------------------------------------------------------
  40.  
  41. const BSTACKSIZE = 70 ; // according to tree.size and alpha
  42.                  // here: tree.size <= 10^9
  43.                  //       alpha = 0.25 ( worst case )
  44.  
  45. enum children     { left = 0 , right = 1 };
  46. enum leaf_or_node { leaf = 0 , node = 1 } ;
  47.  
  48. class ps_node;
  49. class ps_tree;
  50.  
  51. typedef int x_typ;
  52. typedef ps_node* ps_item;
  53.  
  54. struct pair { x_typ x_pkt;
  55.               x_typ y_pkt;
  56.               bool valid;
  57.             };
  58.  
  59. typedef pair* pair_item;
  60.  
  61.  
  62. // -------------------------------------------------------
  63. // class ps_node     
  64. // -------------------------------------------------------
  65.  
  66. class ps_node {
  67.  
  68.   x_typ x_val;
  69.   x_typ y_val;
  70.   x_typ split_val[2];
  71.   int gr;
  72.   ps_item sohn[2];
  73.  
  74.   friend class ps_tree;
  75.  
  76. public:
  77.  
  78.   x_typ x_value()           { return x_val; }
  79.   x_typ y_value()           { return y_val; }
  80.   x_typ split_value_x()     { return split_val[0]; }
  81.   x_typ split_value_y()     { return split_val[1]; }
  82.  
  83.   int blatt()             { return (gr==1); }
  84.   int groesse()           { return gr; }
  85.  
  86.  
  87.   float bal()
  88.   { if (blatt()) return 0.5;
  89.     else return float(float(sohn[left]->groesse())/float(gr));
  90.    }
  91.  
  92.  
  93.   ps_node(x_typ x_v=0,x_typ y_v=0,x_typ split_v_0=0,x_typ split_v_1=0,leaf_or_node ln=leaf,ps_item ls=0,ps_item rs=0)
  94.     { x_val = x_v;
  95.       y_val = y_v;
  96.       split_val[0] = split_v_0;
  97.       split_val[1] = split_v_1;
  98.       sohn[left] = ls;
  99.       sohn[right] = rs;
  100.       if (ln==leaf)
  101.     gr=1;
  102.       else gr = ls->groesse()+rs->groesse();
  103.     }
  104.  
  105.   ps_node(ps_item p)
  106.     { x_val = p->x_value();
  107.       y_val = p->y_value();
  108.       split_val[0] = p->split_value_x();
  109.       split_val[1] = p->split_value_y();
  110.       gr = p->groesse();
  111.       sohn[left] = p->sohn[left];
  112.       sohn[right] = p->sohn[right];
  113.     }
  114.  
  115.   LEDA_MEMORY(ps_node)
  116.       
  117. }; 
  118.  
  119.  
  120. // -------------------------------------------------------
  121. // class ps_tree     
  122. // -------------------------------------------------------
  123.  
  124. class ps_tree {
  125.  
  126.   ps_item root;
  127.   int   anzahl; 
  128.   float alpha;
  129.   float d;
  130.   b_stack<ps_item> st;
  131.  
  132.   friend class ps_node;
  133.  
  134.   void  lrot(ps_item , ps_item ); 
  135.   void  rrot(ps_item , ps_item ); 
  136.   void  ldrot(ps_item , ps_item ); 
  137.   void  rdrot(ps_item , ps_item ); 
  138.  
  139.   ps_item sink(ps_item, x_typ , x_typ);
  140.   void fill(ps_item);
  141.   void delleaf(ps_item);
  142.   void deltree(ps_item);
  143.    
  144.   ps_item search(x_typ, x_typ);
  145.   ps_item locate(x_typ, x_typ);
  146.  
  147.   void enumerate(x_typ, x_typ, x_typ, ps_item);
  148.   void pr_ps_tree(ps_item, int);
  149.   pair_item min_x_in_rect(x_typ ,x_typ ,x_typ ,ps_item);
  150.   pair_item max_x_in_rect(x_typ ,x_typ ,x_typ ,ps_item);
  151.   pair_item min_y_in_xrange(x_typ ,x_typ ,ps_item);
  152.  
  153. public:
  154.  
  155.   virtual int cmp(x_typ x,x_typ y) { return int(x)-int(y); }
  156.   virtual int cmp(x_typ x1,x_typ y1,x_typ x2,x_typ y2) 
  157.                                    { if (int(x1)==int(x2))
  158.                                         return int(y1)-int(y2); 
  159.                                      else return int(x1)-int(x2); }
  160.  
  161.   x_typ   x_value(ps_item it)      { return (it) ? it->x_value() : 0 ; }
  162.   x_typ   y_value(ps_item it)      { return (it) ? it->y_value() : 0 ; }
  163.   x_typ   split_value_x(ps_item it)  { return (it) ? it->split_value_x() : 0 ; }
  164.   x_typ   split_value_y(ps_item it)  { return (it) ? it->split_value_y() : 0 ; }
  165.  
  166.   ps_item insert(x_typ ,x_typ );
  167.   ps_item del(x_typ ,x_typ );
  168.      
  169.   pair_item min_x_in_rect(x_typ x1,x_typ x2,x_typ y0) 
  170.                          { return min_x_in_rect(x1,x2,y0,root); }
  171.   pair_item max_x_in_rect(x_typ x1,x_typ x2,x_typ y0)
  172.                          { return max_x_in_rect(x1,x2,y0,root); }
  173.   pair_item min_y_in_xrange(x_typ x1,x_typ x2)
  174.                            { return min_y_in_xrange(x1,x2,root); }
  175.  
  176.   void enumerate(x_typ x1,x_typ x2,x_typ y0) { enumerate(x1,x2,y0,root); }
  177.  
  178.   void pr_ps_tree() { pr_ps_tree(root,0); }
  179.  
  180.   ps_tree()   :  st(BSTACKSIZE) 
  181.   { root = 0;
  182.     anzahl = 0;
  183.     alpha=0.28;
  184.     d=1/(2-alpha);
  185.    }
  186.  
  187.   virtual ~ps_tree()  
  188.   { if (root)
  189.     { deltree(root);
  190.       delete(root);
  191.      } 
  192.     root = 0; 
  193.     anzahl = 0;
  194.     alpha = 0;
  195.     d = 0;
  196.    }
  197.  
  198. };
  199.  
  200.  
  201. //------------------------------------------------------------------
  202.  
  203. #endif
  204.